package com.imooc.juc.utils;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 资源类
 * @Date:2021/10/26/22:31
 * @Description:
 */
public class Ticket {
    private int num =30;
    private Lock lock = new ReentrantLock();

    public void sale(){
        lock.lock(); // 上锁
        try {
            if (num >0){

                System.out.println(Thread.currentThread().getName() +"卖出第 " + num-- + "还剩 " + num + " 张票；");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();  /// 释放锁
        }
    }

}
